package com.taichuan.selfcheck.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.entity.USBDeviceInfo;
import com.taichuan.selfcheck.mvp.presenter.USBDevicePresenter;
import com.taichuan.selfcheck.mvp.viewinterface.UsbDeviceInterface;
import com.taichuan.selfcheck.ui.adapter.USBDeviceInfoAdapter;
import com.taichuan.selfcheck.ui.dialog.ResultDialog;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import java.util.List;
import butterknife.BindView;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2021/11/5
 */
public class TestUsbDeviceListFragment extends AppBaseFragment<UsbDeviceInterface, USBDevicePresenter> implements UsbDeviceInterface {

    @BindView(R.id.usb_device_count_textView)
    TextView usbDeviceCountTv;
    @BindView(R.id.usb_device_recyclerView)
    RecyclerView usbDeviceRecyclerView;

    ResultDialog resultDialog;
    private USBDeviceInfoAdapter usbDeviceInfoAdapter;

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_usb_device_list;
    }

    @Override
    protected void initView(View view, Bundle bundle) {

        resultDialog = new ResultDialog(PropertyDefaultValue.FUNCTION_TEST_9, getResources().getString(R.string.usb_device_list_test),
                getContext());            //设置弹窗发送的事件——灯光测试

        initRecyclerView();

        //获取设备当前插入的USB设备
        mPresenter.readAllUsbDevice();
    }

    private void initRecyclerView() {
        usbDeviceInfoAdapter = new USBDeviceInfoAdapter();
        usbDeviceRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        usbDeviceRecyclerView.setAdapter(usbDeviceInfoAdapter);
    }

    @Override
    protected USBDevicePresenter createPresenter() {
        return new USBDevicePresenter(getActivity());
    }

    @Override
    public void refreshUsbDeviceInfo(List<USBDeviceInfo> usbDeviceList) {
        usbDeviceInfoAdapter.setNewData(usbDeviceList);
        if (usbDeviceList != null){
            usbDeviceCountTv.setText(usbDeviceList.size() + "个");
        }else {
            usbDeviceCountTv.setText("0个");
        }
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {
        resultDialog.show();
    }

    @Override
    protected void getBundleValues(Bundle bundle) {

    }
}
